package dxn.nl.file.api;

import dxn.boot.common.entity.*;
import dxn.boot.starter.web.control.BaseControl;
import dxn.boot.starter.web.security.AuthUtils;
import dxn.nl.file.biz.Const;
import dxn.nl.file.biz.po.AppModelPo;
import dxn.nl.file.biz.vo.CsApiModeVo;
import dxn.nl.file.service.CsExportAppModelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/mode/base")
@Tag(name = "模块管理", description = "模块管理")
public class AppModelControl extends BaseControl {

    @Resource
    private CsExportAppModelService csExportAppModelService;

    @Operation(summary = "1 添加模块")
    @RequestMapping(value = "/addMode", method = RequestMethod.POST)
    public BaseResult<String> test(@RequestBody @Valid AppModelPo po) {

        AuthUser userDetails = AuthUtils.getCurrentUser();
        if(!Const.topOrgCode.equals(userDetails.getOrgCode()))
        {
            return BaseResult.fail("非总部人员不能编辑");
        }

        po.setCreateTime(LocalDateTime.now());
        po.setCreateUserCode(userDetails.getCode());
        po.setCreateUserName(userDetails.getName());
        po.setUpdateTime(LocalDateTime.now());
        po.setUpdateUserCode(userDetails.getCode());
        po.setUpdateUserName(userDetails.getName());
        String result = csExportAppModelService.addMode(po);

        return checkAndReturnResult(result);
    }

    @Operation(summary = "2 修改模块")
    @RequestMapping(value = "/modifyMode", method = RequestMethod.POST)
    public BaseResult<String> modifyMode(@RequestBody @Valid AppModelPo po) {

        AuthUser userDetails = AuthUtils.getCurrentUser();
        if(!Const.topOrgCode.equals(userDetails.getOrgCode()))
        {
            return BaseResult.fail("非总部人员不能编辑");
        }

        po.setUpdateTime(LocalDateTime.now());
        po.setUpdateUserCode(userDetails.getCode());
        po.setUpdateUserName(userDetails.getName());
        String result = csExportAppModelService.modifyMode(po);

        return checkAndReturnResult(result);
    }

    @Operation(summary = "3 删除模块")
    @RequestMapping(value = "/deleteMode", method = RequestMethod.POST)
    public BaseResult<String> deleteMode(@RequestBody @Valid AppModelPo po) {

        AuthUser userDetails = AuthUtils.getCurrentUser();
        if(!Const.topOrgCode.equals(userDetails.getOrgCode()))
        {
            return BaseResult.fail("非总部人员不能编辑");
        }

        String result = csExportAppModelService.deleteMOde(po);

        return checkAndReturnResult(result);
    }

    @Operation(summary = "4 分页查询模块")
    @RequestMapping(value = "/pageModeByModeVo", method = RequestMethod.POST)
    public BaseResult<List<AppModelPo>> selectAllModeByModeVo(@RequestBody @Valid CsBasePageQuery<CsApiModeVo> pageQuery) {

        AuthUser userDetails = AuthUtils.getCurrentUser();
        if(!Const.topOrgCode.equals(userDetails.getOrgCode()))
        {
            pageQuery.getQuery().setOrgCode(userDetails.getOrgCode());
        }

        CsBasePageResult<AppModelPo> result = csExportAppModelService.pageModeByModeVo(pageQuery);

        return BaseResult.success(result);
    }
}
